﻿using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Handlers;
namespace UcAsp.Net.BLL
{
    public class SettingConfig
    {

        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        private static string config = System.Configuration.ConfigurationManager.AppSettings["Config"];
        /// <summary>
        /// 获取网站设置
        /// </summary>
        public static UcAsp.Net.Model.SiteSetting GetSiteSetting
        {
            get
            {  
                if (Comm.SpaceCache.GetCache("GetSiteSetting") == null)
                {
                    UcAsp.Net.Model.SiteSetting model = new UcAsp.Net.Model.SiteSetting();

                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(path + "\\config\\" + config + "\\SiteSettings.config");
                    string xmlpath = ("Settings");
                    if (xmlDoc.SelectNodes(xmlpath).Count != 0)
                    {
                        XmlNodeList xmlList = xmlDoc.SelectNodes(xmlpath);
                        foreach (XmlNode xns in xmlList)
                        {
                            foreach (XmlNode xn in xns)
                            {
                                for (int i = 0; i < xn.ChildNodes.Count; i++)
                                {
                                    XmlToObject(model, xn.Name, xn.InnerText);
                                }
                            }
                        }
                    }
                    Comm.SpaceCache.CreateCache(model, "GetSiteSetting");
                }
                return (UcAsp.Net.Model.SiteSetting)Comm.SpaceCache.GetCache("GetSiteSetting");
            }
        }

        public static string Theme(string userAgent)
        {
            UcAsp.Net.Model.SiteSetting config = GetSiteSetting;
            if (userAgent.ToLower().IndexOf("android") > 0 || userAgent.ToLower().IndexOf("ios") > 0)
            {
                GetSiteSetting.Theme = GetSiteSetting.MobileTheme;
            }
            else
            {
                GetSiteSetting.Theme = config.Theme;
               
            }
           
            return GetSiteSetting.Theme;
        }
        /// <summary>
        /// 保存网站设置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool SaveSiteSetting(UcAsp.Net.Model.SiteSetting model)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path + "\\config\\" + config + "\\SiteSettings.config");
            string xmlpath = "Settings";
            XmlNode root = xmlDoc.SelectSingleNode(xmlpath);
            Type type = model.GetType();
            System.Reflection.PropertyInfo[] propertys = type.GetProperties();
            foreach (System.Reflection.PropertyInfo t in propertys)
            {
                XmlElement node = (XmlElement)root.SelectSingleNode(t.Name);
                System.Reflection.PropertyInfo property = type.GetProperty(t.Name);
                //当model值为空是跳过
                try
                {
                    node.InnerText = property.GetValue(model, null).ToString();
                }
                catch { }
            }
            xmlDoc.Save(path + "\\config\\" + config + "\\SiteSettings.config");
            Comm.SpaceCache.RemoveAllCache();
            return true;
        }
        protected static void XmlToObject(object oPut, string name, string value)
        {
            System.ComponentModel.PropertyDescriptor pd = System.ComponentModel.TypeDescriptor.GetProperties(oPut).Find(name, true);
            if (pd != null)
            {
                if (pd.PropertyType == typeof(int) || pd.PropertyType == typeof(long) || pd.PropertyType == typeof(string) || pd.PropertyType == typeof(DateTime) || pd.PropertyType == typeof(bool))
                {
                    if (pd.PropertyType == typeof(int))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt32(value));
                    }
                    else if (pd.PropertyType == typeof(long))
                    {
                        pd.SetValue(oPut, System.Convert.ToInt64(value));
                    }
                    else if (pd.PropertyType == typeof(double))
                    {
                        pd.SetValue(oPut, System.Convert.ToDouble(value));
                    }
                    else if (pd.PropertyType == typeof(bool))
                    {
                        pd.SetValue(oPut, System.Convert.ToBoolean(value));
                    }
                    else
                    {
                        if (value == null)
                            pd.SetValue(oPut, string.Empty);
                        else
                            pd.SetValue(oPut, value);
                    }
                }
            }
            else
            {
                //对象为空
            }
        }
        /// <summary>
        /// 获取皮肤地址
        /// </summary>
        /// <param name="control">对应control</param>
        /// <param name="action">对应action</param>
        /// <returns></returns>
        public static string Theme(string control, string action)
        {
            return Theme(null, control, action);
        }
        /// <summary>
        /// 获取皮肤地址
        /// </summary>
        /// <param name="area">对应Areas</param>
        /// <param name="control"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static string Theme(string area, string control, string action)
        {
           
            if (area == null)
            {
                if (File.Exists(path + "/" + SettingConfig.GetSiteSetting.Theme + "/" + control + "/" + action + ".cshtml"))
                {
                    return "~/" + SettingConfig.GetSiteSetting.Theme + "/" + control + "/" + action + ".cshtml";
                }
                else
                {
                    return "~/default/" + control + "/" + action + ".cshtml";
                }
            }
            else
            {
                if (File.Exists(path + "/" + SettingConfig.GetSiteSetting.Theme + "/" + area + "/" + control + "/" + action + ".cshtml"))
                {
                    return "~/" + SettingConfig.GetSiteSetting.Theme + "/" + area + "/" + control + "/" + action + ".cshtml";
                }
                else
                {
                    return "~/default/" + control + "/" + action + ".cshtml";
                }
            }
        }
    }
}